// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
es6id: 25.4.4.5
description: >
  Resolve function is called after Promise constructor returns.
info: |
  Promise.resolve ( x )

  ...
  4. Let promiseCapability be NewPromiseCapability(C).
  5. ReturnIfAbrupt(promiseCapability).
  6. Let resolveResult be Call(promiseCapability.[[Resolve]], undefined, «x»).
  7. ReturnIfAbrupt(resolveResult).
  ...
---*/

function Test() {
    var expectedThisValue = (function() {
      return this;
    }());
    var callCount = 0;
    var object = {};
    var thisValue, args;

    Promise.resolve.call(function(executor) {
      function resolve(v) {
        callCount += 1;
        thisValue = this;
        args = arguments;
      }
      executor(resolve, Test262Error.thrower);
      assert.sameValue(callCount, 0, "callCount before returning from constructor");
    }, object);

    assert.sameValue(callCount, 1, "callCount after call to resolve()");
    assert.sameValue(typeof args, "object");
    assert.sameValue(args.length, 1);
    assert.sameValue(args[0], object);
    assert.sameValue(thisValue, expectedThisValue);
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
